<template>
	<view class="container">
		<Navbar isZhanwei color="#000" title="推广二维码" bg-color="#fff">
			<template #right>
				<u-icon @click="onShowShareSheet()" size="36" name="zhuanfa"></u-icon>
			</template>
		</Navbar>
		<view v-if="imageUrl" class="poster">
			<image class="image" mode="widthFix" :src="imageUrl" @click="onPreviewImage"></image>
		</view>
	</view>
</template>

<script>
	import * as Api from '@/api/dealer/poster'
	import SettingModel from '@/common/model/dealer/Setting'
	import Navbar from '@/components/w-navbar/index.vue'
	export default {
		components: {
			Navbar
		},
		data() {
			return {
				// 海报图url
				imageUrl: undefined
			}
		},
		onShow() {
			uni.hideLoading()
		},

		/**
		 * 生命周期函数--监听页面加载
		 */
		onLoad(options) {
			this.getSetting()
			this.getPoster()
		},

		methods: {
			onShowShareSheet() {
				uni.showLoading({
					title: '打开微信中...'
				})
				uni.share({
					provider: 'weixin',
					scene: 'WXSceneSession',
					type: 2,
					imageUrl: this.imageUrl,
					success(res) {
						console.log("success:" + JSON.stringify(res));
						uni.hideLoading()
					},
					fail(err) {
						console.log(err, 'err======');
						uni.hideLoading()
						if (err.errCode === -8) {
							app.$toast('很抱歉，您的手机没有安装微信~')
						}
					},
					complete: () => {
						uni.hideLoading()
					}
				})
			},

			// 获取分销设置
			getSetting() {
				const app = this
				SettingModel.data()
					.then(setting => {
						const words = setting.words.poster
						app.setPageTitle(words.title)
					})
			},

			// 获取推广二维码
			getPoster() {
				const app = this
				app.isLoading = true
				Api.qrcode({
						channel: 'APP'
					})
					.then(result => {
						// api数据赋值
						app.imageUrl = result.data.imageUrl
					})
					.finally(() => app.isLoading = false)
			},

			// 设置当前页面标题
			setPageTitle(title) {
				uni.setNavigationBarTitle({
					title: title.value
				})
			},

			// 预览海报图
			onPreviewImage() {
				uni.previewImage({
					current: this.imageUrl,
					urls: [this.imageUrl]
				})
			}

		}
	}
</script>

<style>
	page {
		background: #fff;
	}
</style>
<style lang="scss" scoped>
	.poster .image {
		display: block;
		width: 100%;
	}
</style>